import { DirectiveBinding } from 'vue';
import _ from 'lodash';
import $provider from "../provider"
import { DbSourceMethod } from '../enum';

// 创建一个通用的指令工厂函数
export function bindDict() {
    return {
        mounted(el: HTMLElement, binding: DirectiveBinding<string>) {
            // 定义处理事件的函数
            let value = binding.value as any
            if (!value) {
                return
            }

            $provider.meta.getMetaDictById(value).then((res: any) => {
                el.innerText = res?.label || value|| "-";
            })
        },
    };
}
// 创建一个通用的指令工厂函数
export function bindData() {
    return {

        mounted(el: HTMLElement, binding: DirectiveBinding<string>) {
            // 定义处理事件的函数

            let value = binding.value as any
            if (!value || typeof value != "object" || !value.fieldId || !value.value) {
                return
            }
            $provider.meta.getMetaColumn(value.fieldId).then((res: any) => {
                el.innerText = value.value || "-";
                if (res.method == DbSourceMethod.DataDict) {
                    $provider.meta.getMetaDictByValueAndPid(res.param, value.value).then((res: any) => {
                        el.innerText = res?.label || value.value || "-";
                    })
                }
                else if (res.method == DbSourceMethod.DataTable) {
                    $provider.biz.getMetaNameDictById(res.param, value.value).then((res: any) => {
                        el.innerText = res?.label || (value.value == 0 ? "-" : value.value)|| "-";
                    })
                }
            })
        },
    };
}